const { Service } = require('egg');
const ERROR_CODE = require('../errorCode');
const {
  Course,
  ExperimentCourse,
} = require('../db/model');

class ExperimentCourseService extends Service {
  async getAllExperimentCourse() {
    await Course.hasMany(ExperimentCourse);
    await ExperimentCourse.belongsTo(Course, {
      foreignKey: 'cId',
    });
    const res = await ExperimentCourse.findAll({
      attributes: { exclude: ['CourseCId'] },
      include: Course,
    }).catch(err => {
      console.log(`ExperimentCourse 表查找失败: ${err}`);
    });
    if (res === undefined) {
      console.log(`ExperimentCourse 表查找失败: ${ERROR_CODE.DATABASE_ERROR}`);
      return ERROR_CODE.DATABASE_ERROR;
    }
    return res;
  }

  async getExperimentCourse(experimentCourse) {
    if (!experimentCourse?.ecId) {
      console.log(`ExperimentCourse 表查找失败: ${ERROR_CODE.PARAMETERS_MISSING}\n参数: ${experimentCourse}`, experimentCourse);
      return ERROR_CODE.PARAMETERS_MISSING;
    }
    await Course.hasMany(ExperimentCourse);
    await ExperimentCourse.belongsTo(Course, {
      foreignKey: 'cId',
    });
    const res = await ExperimentCourse.findOne({
      where: {
        ecId: experimentCourse?.ecId || -1
      },
      attributes: { exclude: ['CourseCId'] },
      include: Course,
    }).catch(err => {
      console.log(`ExperimentCourse 表查找失败: ${err}\n参数: ${experimentCourse}`, experimentCourse);
    });
    if (res === undefined) {
      console.log(`ExperimentCourse 表查找失败: ${ERROR_CODE.DATABASE_ERROR}\n参数: ${experimentCourse}`, experimentCourse);
      return ERROR_CODE.DATABASE_ERROR;
    }
    if (res === null) {
      console.log(`ExperimentCourse 表查找失败: ${ERROR_CODE.PRIMARY_KEY_NOT_EXIST}\n参数: ${experimentCourse}`, experimentCourse);
      return ERROR_CODE.PRIMARY_KEY_NOT_EXIST;
    }
    return res;
  }

  async appendExperimentCourse(experimentCourse) {
    let res = await this.getExperimentCourse(experimentCourse);
    if (res instanceof Object) {
      res = ERROR_CODE.PRIMARY_KEY_EXIST;
    }
    if (res !== ERROR_CODE.PRIMARY_KEY_NOT_EXIST) {
      console.log(`ExperimentCourse 表新增失败: ${res}\n参数: ${experimentCourse}`, experimentCourse);
      return res;
    }
    res = await ExperimentCourse.create(experimentCourse).catch(err => {
      console.log(`ExperimentCourse 表新增失败: ${err}\n参数: ${experimentCourse}`, experimentCourse);
    });
    if (res === undefined) {
      console.log(`ExperimentCourse 表新增失败: ${ERROR_CODE.DATABASE_ERROR}\n参数: ${experimentCourse}`, experimentCourse);
      return ERROR_CODE.DATABASE_ERROR;
    }
    return res;
  }

  async updateExperimentCourse(experimentCourse) {
    let res = await this.getExperimentCourse(experimentCourse);
    if (Object.values(ERROR_CODE).includes(res)) {
      console.log(`ExperimentCourse 表更新失败: ${res}\n参数: ${experimentCourse}`, experimentCourse);
      return res;
    }
    res = await ExperimentCourse.update(experimentCourse, {
      where: {
        ecId: experimentCourse?.ecId || -1
      },
    }).catch(err => {
      console.log(`ExperimentCourse 表更新失败: ${err}\n参数: ${experimentCourse}`, experimentCourse);
    });
    if (res === undefined) {
      console.log(`ExperimentCourse 表新增失败: ${ERROR_CODE.DATABASE_ERROR}\n参数: ${experimentCourse}`, experimentCourse);
      return ERROR_CODE.DATABASE_ERROR;
    }
    return res;
  }

  async destroyExperimentCourse(experimentCourse) {
    let res = await this.getExperimentCourse(experimentCourse);
    if (Object.values(ERROR_CODE).includes(res)) {
      console.log(`ExperimentCourse 表删除失败: ${res}\n参数: ${experimentCourse}`, experimentCourse);
      return res;
    }
    res = await ExperimentCourse.destroy({
      where: {
        ecId: experimentCourse?.ecId || -1
      },
    }).catch(err => {
      console.log(`ExperimentCourse 表删除失败: ${err}\n参数: ${experimentCourse}`, experimentCourse);
    });
    if (res === undefined) {
      console.log(`ExperimentCourse 表新增失败: ${ERROR_CODE.DATABASE_ERROR}\n参数: ${experimentCourse}`, experimentCourse);
      return ERROR_CODE.DATABASE_ERROR;
    }
    return res;
  }
}

module.exports = ExperimentCourseService;
